<?php

namespace SLN0002\Service;

use Home\Common\FIdConst;
use Home\Service\BizlogService;
use Home\Service\PSIBaseExService;
use Home\Service\UserService;
use SLN0002\DAO\AccCommDAO;
use SLN0002\DAO\VoucherDAO;

/**
 * 记账凭证 Service
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class VoucherService extends PSIBaseExService
{
  private $LOG_CATEGORY = "记账凭证";

  /**
   * 返回所有的公司列表
   *
   * @return array
   */
  public function companyList()
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params = [
      "loginUserId" => $this->getLoginUserId(),
      "fid" => FIdConst::VOUCHER
    ];

    $dao = new VoucherDAO($this->db());
    return $dao->companyList($params);
  }

  /**
   * 凭证主表列表
   */
  public function voucherList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $dao = new VoucherDAO($this->db());
    return $dao->voucherList($params);
  }

  /**
   * 查询凭证字列表
   */
  function queryVoucherWord($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VoucherDAO($this->db());
    return $dao->queryVoucherWord($params);
  }

  /**
   * 查询账样的扩展项
   */
  function queryFmtEx($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VoucherDAO($this->db());
    return $dao->queryFmtEx($params);
  }

  /**
   * 新建或编辑凭证
   */
  public function editVoucher($json)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $bill = json_decode(html_entity_decode($json), true);
    if ($bill == null) {
      return $this->bad("传入的参数错误，不是正确的JSON格式");
    }

    $db = $this->db();

    $db->startTrans();

    $dao = new VoucherDAO($db);

    $us = new UserService();
    $bill["loginUserId"] = $us->getLoginUserId();
    $bill["dataOrg"] = $us->getLoginUserDataOrg();

    $id = $bill["id"];

    if ($id) {
      // 编辑
      $rc = $dao->updateVoucher($bill);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
    } else {
      // 新建
      $bill["createdBy"] = 0; // 手动创建凭证
      $rc = $dao->addVoucher($bill);
      if ($rc) {
        $db->rollback();
        return $rc;
      }

      $id = $bill["id"];
    }

    // 记录业务日志
    $log = $bill["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok($id);
  }

  /**
   * 凭证分录
   */
  public function voucherDetailList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VoucherDAO($this->db());
    return $dao->voucherDetailList($params);
  }

  /**
   * 删除凭证
   */
  public function deleteVoucher($id)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();
    $db->startTrans();

    $dao = new VoucherDAO($db);
    $params = [
      "id" => $id,
    ];
    $rc = $dao->deleteVoucher($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 凭证详情
   *
   * @param array $params
   */
  public function voucherInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VoucherDAO($this->db());
    return $dao->voucherInfo($params);
  }

  /**
   * 复核凭证
   */
  public function commitVoucher($id)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();
    $db->startTrans();

    $dao = new VoucherDAO($db);
    $params = [
      "id" => $id,
      "loginUserId" => $this->getLoginUserId(),
      "loginUserName" => $this->getLoginUserName(),
    ];
    $rc = $dao->commitVoucher($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 取消复核凭证
   */
  public function cancelCommitVoucher($id)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();
    $db->startTrans();

    $dao = new VoucherDAO($db);
    $params = [
      "id" => $id,
    ];
    $rc = $dao->cancelCommitVoucher($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 凭证断号重排
   */
  public function refReorder($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();
    $db->startTrans();

    $dao = new VoucherDAO($db);

    $rc = $dao->refReorder($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 查询当前会计期间
   */
  public function queryCurrentPeriod($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $companyId = $params["companyId"];

    $daoComm = new AccCommDAO($this->db());

    $rc = $daoComm->getCurrentAccYearAndMonth($companyId);
    if (!$rc) {
      return [
        "success" => false,
      ];
    } else {
      return [
        "success" => true,
        "year" => $rc["accYear"],
        "month" => $rc["accMonth"],
      ];
    }
  }

  /**
   * 红冲凭证
   */
  public function discardVoucher($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();
    $params["loginUserName"] = $this->getLoginUserName();
    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $db = $this->db();
    $db->startTrans();

    $dao = new VoucherDAO($db);
    $rc = $dao->discardVoucher($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }
}
